﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace web
{
   

    public class CRule : AuthorizeAttribute
    {
        public string ActionName { get; set; }
        public string ControllerName { get; set; }
        protected string MethodType = string.Empty;
        protected Dictionary<string, string> DicQueryString = new Dictionary<string, string>();
        protected Dictionary<string, string> DicForm = new Dictionary<string, string>();
        protected Dictionary<string, string> DicSession = new Dictionary<string, string>();
        protected Dictionary<string, string> DicCookie = new Dictionary<string, string>();
        protected Dictionary<string, int> DicMethod = new Dictionary<string, int>() { { "GET", 0 }, { "HEAD", 1 }, { "POST", 2 }, { "DEBUG", 3 }, { "PUT", 4 }, { "DELETE", 5 }, { "PATCH", 6 }, { "OPTIONS", 7 } };

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            object loginUser = httpContext.Session["m_login_user"];
            if (null != loginUser)
            {
                return true;
            }
            return false;
        }

        public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
        {
            ActionName = (string)filterContext.RouteData.Values["action"];
            ControllerName = (string)filterContext.RouteData.Values["controller"];

            if (AuthorizeCore(filterContext.HttpContext) != true)
            {
                if ("Login" == ActionName || "Register" == ActionName || "VCode" == ActionName)
                {
                }
                else
                {
                    filterContext.HttpContext.Response.StatusCode = 302;
                    filterContext.Result = new RedirectResult("/C/Login");
                }
            }
        }

        protected void GetServerData(System.Web.Mvc.AuthorizationContext requestContext)
        {
            DicCookie.Clear();
            foreach (string key in requestContext.HttpContext.Request.Cookies.AllKeys)
            {
                DicCookie.Add(key, requestContext.HttpContext.Request.Cookies[key].Value);
            }
            DicForm.Clear();
            foreach (string key in requestContext.HttpContext.Request.Form.AllKeys)
            {
                DicForm.Add(key, requestContext.HttpContext.Request.Form[key]);
            }
            DicSession.Clear();
            foreach (string key in requestContext.HttpContext.Session.Keys)
            {
                object session = requestContext.HttpContext.Session[key];
                DicSession.Add(key, session == null ? "" : session.ToString());
            }
            DicQueryString.Clear();
            foreach (string key in requestContext.HttpContext.Request.QueryString.AllKeys)
            {
                DicQueryString.Add(key, requestContext.HttpContext.Request[key]);
            }
        }
      
    }
}